package net.wds.ssbus.modules.ssbus.util;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.IOException;

public class FtpUtils {
    public static FTPClient connectFtpServer(String url,String userName,String password){
        FTPClient ftpClient = new FTPClient();
        ftpClient.setConnectTimeout(1000*30);//设置连接超时时间
        ftpClient.setControlEncoding("utf-8");//设置ftp字符集
        ftpClient.enterLocalPassiveMode();//设置被动模式，文件传输端口设置
        try {
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//设置文件传输模式为二进制，可以保证传输的内容不会被改变
            ftpClient.connect(url);
            ftpClient.login(userName,password);
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)){
                System.out.println("connect ftp {} failed"+url);
                ftpClient.disconnect();
                return null;
            }
            System.out.println("replyCode==========={}"+replyCode);
        } catch (IOException e) {
            System.out.println("connect fail ------->>>{}"+e.getCause());
            return null;
        }
        return ftpClient;
    }
}
